
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "../include/string_tok.h"

int get_tokens(char **token_list[], char *string, char delimitor)
{
    int i,count=0;
    int j;
    int len=strlen(string);
    char *subs=(char *)malloc((len+1)*sizeof(char));
    for(i=0;i<len;i++)
	if(string[i]==delimitor)
	    count++;
    count++;
    *token_list=(char **)malloc((count)*sizeof(char *));
    count=0;
    j=0;
    for(i=0;i<len;i++)
    {
	if(string[i]==delimitor)
	{
		subs[j]='\0';
		(*token_list)[count]=(char *)malloc((j+1)*sizeof(char));
	    	strcpy((*token_list)[count],subs);
	    	count++;
                j=0;
        }
	else
	{
	    subs[j]=string[i];
	    j++;
	}
    }
    subs[j]='\0';
    (*token_list)[count]=subs;
    strcpy((*token_list)[count],subs);
    count++;
    return count;
}
char *get_filename( char *fullpath)
{
    int len,i,j;
    char *path,*cpstr;
    len=strlen(fullpath);
    cpstr=(char *)malloc(len*sizeof(char));
    j=0;
    for(i=len-1;i>=0;i--)
	if(fullpath[i]=='/')
	{
	    cpstr[j]='\0';
	    break;
	}
	else
	{
	    cpstr[j]=fullpath[i];
	    j++;
	}
    path=(char*)malloc((j+1)*sizeof(char));
    for(i=0;i<j;i++)
    {
	path[i]=cpstr[j-i-1];
    }
    path[j]='\0';
    return path;
}
